﻿/**
* @description  Class to hold collection of style properties (colors and fonts).
*/
package com.foed.graphic {

	public class StyleFormat {
	
		private var _name:String;
		private var _listeners:Array;
		private var _properties:Object;
	
		/**
		* @description  Constructor.
		*
		* @param  pName  The name given to the StyleFormat instance.
		*/
		public function StyleFormat(pName:String) {
			init(pName);
		}
	
		/**
		* @description  Initializes properties.
		*
		* @param  pName  The name of the style format.
		*/
		private function init(pName:String):void {
			_listeners = [];
			_properties = {};
			_name = pName;
		}
		
		/**
		* @description  Sets the value for a specified property.
		*
		* @param  pKey  The name of the property to set.
		* @param  pValue  The value for the property.
		*/
		public function setProperty(pKey:String, pValue:Object):void {
			_properties[pKey] = pValue;	
		}

		/**
		* @description  Retrieves the value for a specified property.
		*
		* @param  pKey  The name of the property to retrieve.
		*
		* @returns  The value of the specified property.
		*/
		public function getProperty(pKey:String):Object {
			return _properties[pKey];	
		}
	
		/**
		* @description  Adds a listener for updates to the style.
		*
		* @param  pListener  The IStyleable instance to listen for updates.
		*/
		public function addListener(pObj:IStyleable):void {
			var pFormat:StyleFormat = pObj.getStyleFormat();
			if (pFormat != null) {
				pFormat.removeListener(pObj);
			}
			pObj.setStyleFormat(this);
			for each (var i:IStyleable in _listeners) {
				if (i == pObj) return;
			}
			_listeners.push(pObj);
		}
	
		/**
		* @description  Removes listener of style updates.
		*
		* @param  pListener  The IStyleable instance to remove.
		*/
		public function removeListener(pObj:IStyleable):void {
			var pLength:uint = _listeners.length;
			for (var i:uint = 0; i < pLength; i++) {
				if (_listeners[i] == pObj) {
					_listeners.splice(i, 1);
					break;
				}
			}
			pObj.clearStyleFormat();
		}
	
		/**
		* @description  Updates all listeners of the style format with changes.
		*/
		public function updateStyles():void {
			for each (var i:IStyleable in _listeners) {
				i.setStyleFormat(this);
			}
		}
		
		/**
		* @description  Returns the name given to the style format.
		*
		* @returns  The name given to the style format.
		*/
		public function get name():String {
			return _name;	
		}
	
	}
	
}